package com.test08.excelpoi01;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;

import org.junit.Test;

import jxl.Cell;
import jxl.CellType;
import jxl.Sheet;
import jxl.Workbook;
import jxl.write.Label;
import jxl.write.WritableCell;
import jxl.write.WritableSheet;
import jxl.write.WritableWorkbook;

/*
 * 用来读取一个excel中的内容
 * 
 */
public class ExcelTest01 {
	public static void main(String[] args)
	{
		jxl.Workbook readwb = null;
		try
		{
			// 构建Workbook对象, 只读Workbook对象
			// 直接从本地文件创建Workbook
			InputStream instream = new FileInputStream("D:\\1.xls");
			readwb = Workbook.getWorkbook(instream);

			// Sheet的下标是从0开始
			// 获取第一张Sheet表
			Sheet readsheet = readwb.getSheet(0);
			// 获取Sheet表中所包含的总列数
			int rsColumns = readsheet.getColumns();
			// 获取Sheet表中所包含的总行数
			int rsRows = readsheet.getRows();
			// 获取指定单元格的对象引用
			for (int i = 3; i < rsRows; i++)
			{
				for (int j = 0; j < rsColumns; j++)
				{
					Cell cell = readsheet.getCell(j, i);
					System.out.print(cell.getContents() + " ");
				}
				System.out.println();

			}


		} catch (Exception e) {

			e.printStackTrace();

		} finally {

			readwb.close();

		}
	}
	@Test
	public void get() throws Exception{
//		创建一个excel文件对象(这个是只读的)
		Workbook workbook = Workbook.getWorkbook(new File("src/1.xls"));

		// 获取第一张Sheet表
		Sheet readsheet = workbook.getSheet(0);
		// 获取Sheet表中所包含的总列数
		int rsColumns = readsheet.getColumns();
		// 获取Sheet表中所包含的总行数
		int rsRows = readsheet.getRows();
		// 获取指定单元格的对象引用
		for (int i = 0; i < rsRows; i++) {
			for (int j = 0; j < rsColumns; j++) {
				Cell cell = readsheet.getCell(j, i);
				System.out.print(cell.getContents() + " ");
			}
			System.out.println();
		}
		workbook.close();
	}
	@Test
	public void put() throws Exception {
		//创建工作薄
        WritableWorkbook workbook = Workbook.createWorkbook(new File("src/2.xls"));
        //创建新的一页
        WritableSheet sheet = workbook.createSheet("First Sheet",0);
        //创建要显示的内容,创建一个单元格，第一个参数为列坐标，第二个参数为行坐标，第三个参数为内容
        Label xuexiao = new Label(0,0,"学校");
        sheet.addCell(xuexiao);
        Label zhuanye = new Label(1,0,"专业");
        sheet.addCell(zhuanye);
        Label jingzhengli = new Label(2,0,"专业竞争力");
        sheet.addCell(jingzhengli);
        
        Label qinghua = new Label(0,1,"清华大学");
        sheet.addCell(qinghua);
        Label jisuanji = new Label(1,1,"计算机专业");
        sheet.addCell(jisuanji);
        Label gao = new Label(2,1,"高");
        sheet.addCell(gao);
        
        Label beida = new Label(0,2,"北京大学");
        sheet.addCell(beida);
        Label falv = new Label(1,2,"法律专业");
        sheet.addCell(falv);
        Label zhong = new Label(2,2,"中");
        sheet.addCell(zhong);
        
        Label ligong = new Label(0,3,"北京理工大学");
        sheet.addCell(ligong);
        Label hangkong = new Label(1,3,"航空专业");
        sheet.addCell(hangkong);
        Label di = new Label(2,3,"低");
        sheet.addCell(di);
        
        //把创建的内容写入到输出流中，并关闭输出流
        workbook.write();
        workbook.close();
	}
}
